package com.hqyj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.pojo.EmployeeContract;
import com.hqyj.dao.EmployeeContractMapper;
import com.hqyj.service.IEmployeeContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-20
 */
@Service
public class EmployeeContractServiceImpl extends ServiceImpl<EmployeeContractMapper, EmployeeContract> implements IEmployeeContractService {
    //注入mapper
    @Autowired(required = false)
    EmployeeContractMapper employeeContractMapper;
    @Override
    public HashMap<String, Object> select(EmployeeContract e) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        System.out.println(e.getSort()+"-----------");
        //定义分页对象
        Page<EmployeeContract> page = new Page<>(e.getPage(),e.getRow());
        //条件构造器
        QueryWrapper<EmployeeContract> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(e.getContractId()!=null,"contract_id",e.getContractId());
        queryWrapper.like(e.getEmployeeId()!=null&&!e.getEmployeeId().equals(""),"employee_id",e.getEmployeeId());

                if (e.getSort().equals("asc")) {
                    //升序
                    queryWrapper.orderByAsc("contract_start_date");

                } else if (e.getSort().equals("desc")){
                    //降序
                    queryWrapper.orderByDesc("contract_start_date");

                }else if (e.getSort().equals("asc1")){
                    //降序
                    queryWrapper.orderByAsc("contract_end_date");

                }else if (e.getSort().equals("desc1")){
                    //降序
                    queryWrapper.orderByDesc("contract_end_date");

                }

        //查询数据库
        Page<EmployeeContract> eList =employeeContractMapper.selectPage(page,queryWrapper);
        //当前页集合
        map.put("list",eList.getRecords());
        //总页数
        map.put("totalPage",eList.getPages());
        //总条数
        map.put("total",eList.getTotal());
        //当前页
        map.put("curPage",e.getPage());
        if(e.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",e.getPage()-1);

        }
        if(e.getPage()==eList.getPages()){
            //下一页
            map.put("nextPage",eList.getPages());

        }else{
            map.put("nextPage",e.getPage()+1);

        }
        //每页显示条数
        map.put("row",e.getRow());

        return map;
    }

    @Override
    public HashMap<String, Object> add(EmployeeContract e) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = employeeContractMapper.insert(e);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;

    }

    @Override
    public HashMap<String, Object> update(EmployeeContract e) {
        HashMap<String, Object> map = new HashMap<String, Object>();

        //修改
        int num = employeeContractMapper.updateById(e);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer id) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num = employeeContractMapper.deleteById(id);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }
    @Override
    public List<EmployeeContract> selectAll(EmployeeContract e) {
        List<EmployeeContract> list = employeeContractMapper.selectAll();

        return list;
    }

}

